<?php
namespace BP\DA;
require_once("java/io/BufferedReader.php");
require_once("java/io/BufferedWriter.php");
require_once("java/io/ByteArrayInputStream.php");
require_once("java/io/File.php");
require_once("java/io/FileInputStream.php");
require_once("java/io/FileOutputStream.php");
require_once("java/io/IOException.php");
require_once("java/io/InputStreamReader.php");
require_once("java/io/OutputStreamWriter.php");
require_once("java/io/StringWriter.php");
require_once("java/util/ArrayList.php");
require_once("java/util/Hashtable.php");
require_once("java/util/Iterator.php");
require_once("java/util/List.php");
require_once("org/dom4j/Attribute.php");
require_once("org/dom4j/Document.php");
require_once("org/dom4j/DocumentHelper.php");
require_once("org/dom4j/Element.php");
require_once("org/dom4j/io/OutputFormat.php");
require_once("org/dom4j/io/SAXReader.php");
require_once("org/dom4j/io/XMLWriter.php");
require_once("BP/Tools/StringHelper.php");
class DataSet {
	protected $name;	// String
	public $Tables;	// List<DataTable>
	public $hashTables;	// Hashtable<String, DataTable>
	public static function constructor__ () 
	{
		$me = new self();
		if (($me->Tables == NULL))
		{
			$me->Tables = []; //new ArrayList();
			$me->hashTables = []; //map
		}
		return $me;
	}
	public function removeTableByName ($tableName) // [String tableName]
	{
		foreach ($this->Tables as $dtb) 		{
			if ($tableName->equals($dtb->getTableName()))
			{
				$this->Tables->remove($dtb);
				return  TRUE ;
			}
		}
		return  FALSE ;
	}
	public function GetTableByName ($tableName) // [String tableName]
	{
		foreach ($this->Tables as $dtb) 		{
			if ($tableName->equals($dtb->getTableName()))
			{
				return $dtb;
			}
		}
		return NULL;
	}
	public static function constructor__String ($name) // [String name]
	{
		$me = new self();
		if (($me->Tables == NULL))
		{
			$me->Tables = []; //new ArrayList();
			$me->hashTables = []; //map
		}
		$me->name = $name;
		return $me;
	}
	public function WriteXml_String ($file) // [String file]
	{
			/* match: String_XmlWriteMode_DataSet */
		$this->WriteXml_String_XmlWriteMode_DataSet($file, XmlWriteMode::$IgnoreSchema, DataSet::constructor__String("NewDataSet"));
	}
	public function WriteXml_String_XmlWriteMode_DataSet ($path, $mode, $ds) // [String path, XmlWriteMode mode, DataSet ds]
	{
		$str = "<?xml version=\"1.0\" standalone=\"yes\"?>";
		$str.="<NewDataSet>";
		for ($i = 0; ($i < $ds->Tables->size()); ++$i) 
		{
			$dt = $ds->Tables->get($i);
			for ($k = 0; ($k < $dt->Rows->size()); ++$k) 
			{
				$str.="<";
				$str.=($dt->TableName);
				$str.=">";
				for ($j = 0; ($j < $dt->Columns->size()); ++$j) 
				{
					$dc = $dt->Columns->get($j);
					$str.="<";
					$str.=($dc->ColumnName);
					$str.=">";
					try 
					{
						$value = $dt->Rows->get($k)->getValue($dc);
						if ((((($value->contains(">") || $value->contains("<")) || $value->contains("&")) || $value->contains("'")) || $value->contains("\"")))
						{
							$value = $value->replace(">", "&gt;");
							$value = $value->replace("<", "&lt;");
							$value = $value->replace("&", "&amp;");
							$value = $value->replace("'", "&apos;");
							$value = $value->replace("\"", "&quot;");
						}
						$str.=$value;
					}
					catch (\Exception $e)
					{ /* empty */ }
					$str.=("</");
					$str.=($dc->ColumnName);
					$str.=(">");
				}
				$str.=("</");
				$str.=($dt->TableName);
				$str.=(">");
			}
		}
		$str.=("</NewDataSet>");
		$temp = $str;
		$file = $path;
		try 
		{
			if (!file_exists($file))
			{
				fopen($file, "w+");
				fwrite($file, $temp);
			}

		}
		catch (\Exception $e)
		{
			$e->getMessage(); // 
		}
	}
	public function formatXml ($str) // [String str]
	{
		$document = NULL;
		$document = $DocumentHelper->parseText($str);
		$format = $OutputFormat->createPrettyPrint();
		$format->setEncoding("gbk");
		$writer = new StringWriter();
		$xmlWriter = new XMLWriter($writer, $format);
		$xmlWriter->write($document);
		$xmlWriter->close();
		return $writer;
	}
	public function readXmls ($xmlPath) // [String xmlPath]
	{
		if (is_null($xmlPath) || $xmlPath == '')
		{
			return ;
		}
		$reader = new SAXReader();
		$file = new File($xmlPath);
		if ($file->exists())
		{
			$document = $reader->read($file);
			$root = $document->getRootElement();
			for ($i = $root->elementIterator(); $i->hasNext(); ) 
			{
				$e = $i->next();
				$type =  FALSE ;
				for ($k = 0; ($k < $this->Tables->size()); ++$k) 
				{
					if ($this->Tables->get($k)->TableName->equals($e->getName()))
					{
						$dt = $this->Tables->get($k);
						$dr = $dt->NewRow();
						$dc = NULL;
						for ($j = $e->elementIterator(); $j->hasNext(); ) 
						{
							$cn = $j->next();
							$dc = DataColumn::constructor__String($cn->getName());
							$dr->setValue_DataColumn_Object($dc, $cn->getText());
						}
						$dt->Columns->Add($dc);
						$dt->Rows->add($dr);
						$type =  TRUE ;
						break;
					}
				}
				if ($type)
				{
					continue;
				}
				$dt = DataTable::constructor__String($e->getName());
				$dr = $dt->NewRow();
				$dc = NULL;
				for ($j = $e->elementIterator(); $j->hasNext(); ) 
				{
					$cn = $j->next();
					$dc = DataColumn::constructor__String($cn->getName());
					$dt->Columns->Add($dc);
					$dr->setValue_DataColumn_Object($dc, $cn->getText());
				}
				$dt->Rows->add($dr);
				$this->Tables->add($dt);
				$this->hashTables->put($e->getName(), $dt);
			}
		}
	}
	public function readXmlm ($xml) // [String xml]
	{
	    if (is_null($xml) || $xml == '')
			return ;
		try 
		{
			$xml = (("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n <NewDataSets>" . $xml) . "</NewDataSets>");
			$reader = new SAXReader();
			$document = NULL;
			$document = $reader->read(new ByteArrayInputStream($xml->getBytes("UTF-8")));
			$elements = $document->selectNodes("//NewDataSets/NewDataSet");
			$i = 0;
			$oratb = DataTable::constructor__();
			foreach ($elements as $element) 			{
				$dr = $oratb->NewRow();
				$iter = $element->elementIterator();
				$j = 0;
				while ($iter->hasNext()) 
				{
					$itemEle = $iter->next();
					if (($i == 0))
					{
						$oratb->Columns->Add($itemEle->getName());
					}
					$dr->setValue_I_Object($j, $itemEle->getTextTrim());
					++$j;
				}
				$oratb->Rows->add($dr);
				++$i;
			}
			$this->Tables->add($oratb);
		}
		catch (\Exception $e)
		{
			$e->getMessage(); // 
		}
	}
	public function getName () 
	{
		return $this->name;
	}
	public function setName ($name) // [String name]
	{
		$this->name = $name;
	}
	public function getTables () 
	{
		return $this->Tables;
	}
	public function setTables ($tables) // [List<DataTable> tables]
	{
		$this->Tables = $tables;
	}
	public function getHashTabless () 
	{
		return $this->hashTables;
	}
	public function setHashTables ($hashTables) // [Hashtable<String, DataTable> hashTables]
	{
		$this->hashTables = $hashTables;
	}
	public function xmlToString ($path) // [String path]
	{
		$line = NULL;
		$strBuffer = "";
		try 
		{
			$encoding = "UTF-8";
			$file = $path;
			if ((is_file($file) && file_exists($file)))
			{
			    $handle = fopen($path, 'r');
			
			    if($handle){
			        while ((($line = fgets($handle)) !== FALSE))
			        {
			            $strBuffer.=(($line . "\n"));
			        }
			         fclose($handle);
			    }
			}
			else
			{
				print ( ("找不到指定的文件�?" . $path) . "\n");
			}
		}
		catch (\Exception $e)
		{
			print ( ("读取文件内容操作出错�?" . $e->getMessage()) . "\n");
		}
		return $strBuffer;
	}
	public function readXml ($xmlpath) // [String xmlpath]
	{
	    if (is_null($xmlpath) || $xmlpath == '')
		{
			return ;
		}
		try 
		{
			$xml = $this->xmlToString($xmlpath);
			$document = new SAXReader()->read(new ByteArrayInputStream($xml->getBytes("UTF-8")));
			$element = $document->getRootElement();
			for ($iterator = $element->elementIterator(); $iterator->hasNext(); ) 
			{
				$el = $iterator->next();
				$dt = $this->hashTables->get($el->getName());
				if (($dt == NULL))
				{
					$dt = DataTable::constructor__String($el->getName());
					$this->hashTables->put($el->getName(), $dt);
					$this->Tables->add($dt);
				}
				$dr = $dt->NewRow();
				$dt->Rows->add($dr);
				for ($it = $el->attributeIterator(); $it->hasNext(); ) 
				{
					$at = $it->next();
					if (!$this->isContains($dt->Columns->subList(0, $dt->Columns->size()), $at->getName()))
					{
						$dt->Columns->Add($at->getName());
					}
					$dr->setValue_String_Boolean($at->getName(), $at->getValue());
				}
				for ($it = $el->elementIterator(); $it->hasNext(); ) 
				{
					$at = $it->next();
					if (!$this->isContains($dt->Columns->subList(0, $dt->Columns->size()), $at->getName()))
					{
						$dt->Columns->Add($at->getName());
					}
					$value = $at->getText();
					try 
					{
						if ((((($value->contains("&gt;") || $value->contains("&lt;")) || $value->contains("&amp;")) || $value->contains("&apos;")) || $value->contains("&quot;")))
						{
							$value = $value->replace("&amp;", "&");
							$value = $value->replace("&gt;", ">");
							$value = $value->replace("&lt;", "<");
							$value = $value->replace("&apos;", "'");
							$value = $value->replace("&quot;", "\"");
						}
					}
					catch (\Exception $e)
					{ /* empty */ }
					$dr->setValue($at->getName(), $value);
				}
			}
		}
		catch (\Exception $e)
		{
			$e->getMessage(); // 
		}
	}
	public function isContains ($dcList, $column) // [List<DataColumn> dcList, String column]
	{
		foreach ($dcList as $dc) 		{
			if ($dc->ColumnName->equals($column))
			{
				return  TRUE ;
			}
		}
		return  FALSE ;
	}
	public static function ConvertDataSetToXml ($dataSet) // [DataSet dataSet]
	{
		if (($dataSet != NULL))
		{
			$str = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
			$str += "<xs:schema id=\"NewDataSet\" xmlns=\"\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\">";
			$str += "<xs:element name=\"NewDataSet\" msdata:IsDataSet=\"true\" msdata:UseCurrentLocale=\"true\">";
			$str += "<xs:complexType><xs:choice minOccurs=\"0\" maxOccurs=\"unbounded\">";
			for ($n = 0; ($n < $dataSet->getTables()->size()); ++$n) 
			{
				$str += (("<xs:element name=\"" . $dataSet->getTables()->get($n)->TableName) . "\">");
				$str += "<xs:complexType><xs:sequence>";
				$table = $dataSet->getTables()->get($n);
				foreach ($table->Columns as $col) 				{
					$str += ("<xs:element name=\"" . $col->ColumnName);
					$str += "\" type=\"";
					try 
					{
						$col->DataType;
						$str += DataSet::IsType($col->DataType);
					}
					catch (\Exception $e)
					{
						$str += "xs:string";
					}
					$str += "\" minOccurs=\"0\" />";
				}
				$str += "</xs:sequence></xs:complexType></xs:element>";
			}
			$str += "</xs:choice></xs:complexType></xs:element><_NewDataSet>";
			for ($i = 0; ($i < $dataSet->getTables()->size()); ++$i) 
			{
				$dt = $dataSet->getTables()->get($i);
				$table = $dataSet->getTables()->get($i);
				for ($j = 0; ($j < $table->Rows->size()); ++$j) 
				{
					$str += (("<_" . $i) . ">");
					$row = $table->Rows->get($j);
					for ($a = 0; ($a < $row->columns->size()); ++$a) 
					{
						$col = $table->Columns->get($a);
						$str += (("<_" . $a) . ">");
						if (($row->getValue_DataColumn($col) == NULL))
						{
							$str += "";
						}
						else
						{
							if ($col->ColumnName->equals("icon"))
							{
								if ($row->getValue_DataColumn($col)->equals(""))
								{
									$str += "审核";
								}
								else
									if ($row->getValue_DataColumn($col)->equals("Default"))
									{
										$str += "审核";
									}
									else
									{
										$str += $row->getValue_DataColumn($col);
									}
							}
							else
							{
								$str += $row->getValue_DataColumn($col);
							}
						}
						$str += (("</_" . $a) . ">");
					}
					$str += (("</_" . $i) . ">");
				}
			}
			$str += "</_NewDataSet>";
			$str += "</xs:schema>";
			return $str;
		}
		return NULL;
	}
	public static function IsType ($name) // [String name]
	{
		if ($name->equals("class java.lang.Integer"))
		{
			$name = "xs:int";
		}
		if ($name->equals("class java.lang.Long"))
		{
			$name = "xs:long";
		}
		if ($name->equals("class java.lang.Float"))
		{
			$name = "xs:float";
		}
		if ($name->equals("class java.lang.Double"))
		{
			$name = "xs:double";
		}
		if ($name->equals("class java.lang.String"))
		{
			$name = "xs:string";
		}
		if ($name->equals("class java.math.BigDecimal"))
		{
			$name = "xs:int";
		}
		return $name;
	}
	public static function main ($args) // [String[] args]
	{
		$set = DataSet::constructor__();
		$set->readXml("D:/JFlow/JFlow/jflow-web/src/main/webapp/WF/Data/FlowDemo/Flow/01.线�?�流�?/表单数据copy测试案例.xml");
		$set2 = DataSet::constructor__();
		$set2->readXml("D:/JFlow/JFlow/jflow-web/src/main/webapp/DataUser/XML/RegularExpression.xml");
		print ( "\n");
	}
}
?>
